<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Action</title>
  </head>
  <body>
    <div id="app">
      <p>count:{{count}}</p>
      <button @click="increaseInMethod({amount:3})">increaseInMethod</button>
      <button @click="increaseAsyncInMethod">increaseAsyncInMethod</button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="https://unpkg.com/vuex@3.1.3/dist/vuex.js"></script>
    <script>
      var store = new Vuex.Store({
        state: { count: 1 },
        mutations: {
          increase(state,payload={amount:1}) {
            //此处只能放同步代码，不能放异步代码
            state.count+=payload.amount;
          },
        },
        actions: {
          increaseInAction(context,payload) {
            //context=store
            context.commit("increase",payload);
          },
          increaseAsyncInAction(context) {
            //context=store
            setTimeout(() => {
              context.commit("increase");
            }, 1000);
          },
        },
      });
      var vm = new Vue({
        store,
        el:'#app',
        computed:{
          count(){
            return this.$store.state.count;
          }
        },
        methods:{
          //...Vuex.mapActions(['increaseInAction','increaseAsyncInAction'])
          ...Vuex.mapActions({
            increaseInMethod:'increaseInAction',
            increaseAsyncInMethod:'increaseAsyncInAction'
          })
         /*  increaseInMethod(payload){
            this.$store.dispatch('increaseInAction',payload);
          },
          increaseAsyncInMethod(){
            //向仓库派发一个动作
            this.$store.dispatch('increaseAsyncInAction');
          } */
        }
      });
    </script>
  </body>
</html>
